<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Wxapp extends Controller
{
    public function index()
    {
        $adminId = session('uid','','admin');
        if(!$adminId)
        {
            return;
        }
        else
        {
            $type = '徐总';
            $list = Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->where('type',$type);
            if(input('keyword'))
            {
                $list = $list->where('name','like','%'.input('keyword').'%');
            }
            if(input('status_text'))
            {
                $list = $list->where('status_text',input('status_text'));
            }
            else 
            {
                $list = $list->where('status_text','<>','已注销')->where('status_text','<>','封主体')->where('status_text','<>','永久封');
            }
            if(input('address'))
            {
                $list = $list->where('address',input('address'));
            }

            if(input('status') != '')
            {
                if(input('status') != 2)
                    $list = $list->where('status',input('status'));
                else
                    $list = $list->where('red',1);
            }
            $list = $list->order('status DESC,id DESC')->select();
            $time = time();
            $voteCount = 0;
            foreach ($list as $k=>$v) {
                // if($v['admin_id'])
                // {
                //     if(!$v['admin_name'])
                //         $list[$k]['admin_name'] = db('admin')->where('id',$v['admin_id'])->value('name');
                //     if($v['admin_id'] == $adminId)
                //         $list[$k]['can_edit'] = 1;
                // }
                if($v['status_text']=='投票专属' )
                {
                    $voteCount += Db::connect("live_db_config")
                            ->table('ims_wzbagent_lives')
                            ->alias('l')
                            ->join('ims_wzbagent_vote_setting v','l.id = v.live_id')
                            ->where('l.wxapp_id',$v['id'])
                            ->where('v.start_at','<',$time)
                            ->where('v.end_at','>',$time)
                            ->count(); 
                }

            }
            if(session('group_id','','admin') == 1 || session('group_id','','admin') == 5)
            {
                $isSuper = 1;
            }
            $this->assign([
                'list'=>$list,
                'manageList'=>$manageList,
                'isSuper'=>$isSuper,
                'voteCount'=>$voteCount
            ]);
            return view();
            //query("SELECT * FROM ims_wzbagent_lives where id IN(4542,4545,4546,4547,4599);");
        }
    }
    
    public function edit()
    {
        
        
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
                if(input('id'))
                {
                    $data['last_time'] = strtotime($data['last_time']);
                    $sqlStatus = Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->where(['id'=>input('id')])->update($data);
                }
                else
                {
                    $sqlStatus = Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->insert($data);
                }
                if($sqlStatus  )
                {
                    return ['msg'=>'操作成功','icon'=>1];
                }
                else
                {
                    return ['msg'=>'操作失败','icon'=>2];
                }
            }
            else
            {
                if(input('id'))
                {
                    $wxappInfo = Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->where(['id'=>input('id')])->find();
                }
                $staffList = db('admin')->where('group_id','IN','2,5')->where('status',1)->select();
                //if(!$wxappInfo['admin_name'])
                //    $wxappInfo['admin_name'] = db('admin')->where('id',$wxappInfo['admin_id'])->value('name');
                if($wxappInfo['last_time'])
                    $wxappInfo['last_time'] = date('Y-m-d H:i:s' , $wxappInfo['last_time']);
                $this->assign([
                    'staffList'=>$staffList,
                    'wxappInfo'=>$wxappInfo,
                ]);
                return view();
            }
        
        
    }
    
    public function liveList()
    {
        if(input('id'))
        {
            $id = input('id');
            $specialList = Db::connect("live_db_config")->table('ims_special')->where('wxapp_id',$id)->order('special_id DESC')->select();
            $liveList =  Db::connect("live_db_config")
                        ->table('ims_wzbagent_lives')
                        ->alias('l')
                        ->join('ims_wzbagent_managers m','l.admin_id = m.id')
                        ->field('l.id,l.title,m.name')
                        ->where('l.wxapp_id',$id)
                        ->order('l.id DESC')
                        ->select();
            foreach ($liveList as $k=>$v)
            {
                $voteSetting = Db::connect("live_db_config")->table('ims_wzbagent_vote_setting')->where('live_id',$v['id'])->find();
                $liveList[$k]['end_at'] = $voteSetting['end_at'];
                $liveList[$k]['start_at'] = $voteSetting['start_at'];
                if($liveList[$k]['end_at'] && $liveList[$k]['end_at']<time())
                {
                    $liveList[$k]['end_at'] = $liveList[$k]['start_at'] = '';
                    $liveList[$k]['end_text'] = '已结束';
                }
                    
            }
            array_multisort(array_column($liveList,'end_at'), SORT_DESC,$liveList);
            $this->assign([
                'specialList'=>$specialList,
                'liveList'=>$liveList,
            ]);
            return view();
        }
    }
    
    
    public function changeStatus()
    {
        if(request()->isAjax()){
            $id=input('id');
            $status = input('status');
            $field = input('field');
            if($status == 1){
                Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->where(['id'=>$id])->update([$field=>0]);
                return ['msg'=>'已禁用','icon'=>2];
            }else{
                Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->where(['id'=>$id])->update([$field=>1]);
                return ['msg'=>'已启用','icon'=>1];
            }
        }else{
            $this->error("非法操作！");
        }
    }
    

    
}